package com.craftplay.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 工具方法
 */
public class ToolUntils {
    private static Logger logger = Logger.getLogger(ToolUntils.class);

    /**
     * 手機號是否正確
     *
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();

            return isMatch;
        }

    }

    /**
     * 遍历实体类
     *
     * @param model
     * @throws Exception
     */
    public static void testReflect(Object model) {
        JSONObject jsonObject = new JSONObject();
        for (Field field : model.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            try {
                jsonObject.put(field.getName(), field.get(model));
            } catch (IllegalAccessException e) {
                logger.error("遍历实体类失败");
                e.printStackTrace();
            }
        }
        logger.info(jsonObject.toJSONString());
    }
}
